/******************************************************************************
 * (C) Copyright 2020 Atech-Automotive
 * FILE NAME:    app_sci1_Itf.c
 * DESCRIPTION:
 * 
 * DATE BEGUN:   2020/09/18
 * BY:           yong.ding
 * PRODUCT NAME:
 * APPLICATION:
 * TARGET H/W:
 * DOC REF:
 *****************************************************************************
 */

/*****************************************************************************
** #include 
*****************************************************************************/
#include <string.h>
#include "app_sci1_Itf.h"
#include "drv_SCI.h"
#include  "msg_sciApi.h"
#include  "msg_canApi.h"
#include "hwa_outputs.h"
#include "hwa_inputs.h"
#define CAN_RX_USE_TEST  1
#define CAN_RX_USE_X70  0

/*****************************************************************************
** function prototypes
*****************************************************************************/
static void sci1ItfGetmessageFromCanMsg123(BOOL sended_flg);
static void sci1ItfGetmessageFromCanMsg124(BOOL sended_flg);
static void app_sci1ItfWriteWFeedbackFlag(UINT8 id,UINT8 sub_id,BOOL set);

/****************************************************************************/
BOOL app_sci1ItfGetIGN(void)
{
    return HWA_GetInput(SI_PP0_IN);
}


BOOL app_sci1ItfSendFrame(UINT8 id,BOOL sub_exist,UINT8 sub_id,UINT8 txData_len,UINT8* p_u8_txData)
{
	return drv_sci1SendFrame(id,sub_exist,sub_id,txData_len,p_u8_txData);
}

BOOL app_sci1ItfGetMessage(UINT8 *u8_id,UINT8 *u8_sub_id, UINT8 *u8_length, UINT8 *p_u8_txData)
{
    return drv_sci1_v_getMessage(u8_id,u8_sub_id,u8_length,p_u8_txData);
}

UINT8 app_sci1ItfGetRxMessageCount(void)
{
    return drv_sci1_u8_getRxMessageCount();  
}

/******************************************************************************/
/**
 * Function Name: app_sci1CheckTimeout
 * Description: none
 *
 * Param:	none
 * Return:	none
 * _____________________________________________________________________________
 *	   Date 			Changes Description 			   Author
 *	09/13/2020				        					yong.ding
 ******************************************************************************/

static void app_sci1ItfWriteWFeedbackFlag(UINT8 id,UINT8 sub_id,BOOL set)
{
	SCI_RX_MSG_S **ptr;
	SCI_RX_LONG_MSG_S **ptr2;
	ptr = p_s_sci1RxTable;
	while((*ptr)->Id != 0)
    {   
        if((*ptr)->SendType == SCI_EVENT &&((*ptr)->Id==id)&&((*ptr)->Sub_Id==sub_id))
        {
            (*ptr)->Waite_Feedback_Flag = set;
			return ;
        }
        ptr++;
    }

	ptr2 = p_s_sci1LongRxTable;
	while((*ptr2)->Id != 0)
    {   
        if((*ptr2)->SendType == SCI_EVENT &&((*ptr2)->Id==id)&&((*ptr2)->Sub_Id==sub_id))
        {
            (*ptr2)->Waite_Feedback_Flag = set;
			return ;
        }
        ptr2++;
    }
}


void app_sci1ItfEventSent(SCI_TX_MSG_S *ptr)
{

	if((ptr)->SendType == SCI_EVENT)
	{
		if((ptr)->Trigger)
		{
			/*send msg*/
			if (app_sci1ItfSendFrame((ptr)->Id,(ptr)->Sub_Id_exist,(ptr)->Sub_Id,(ptr)->Length,(ptr)->Data) == FALSE)
			{

			}
			else
			{
				/* transmit success */
				(ptr)->Trigger = FALSE;
				app_sci1ItfWriteWFeedbackFlag((ptr)->Id,(ptr)->Sub_Id,TRUE);//set Waite_Feedback_Flag
				if((ptr)->NobackRetry_cn >= (ptr)->NobackRetry_cn_max)
				{
					app_sci1ItfWriteWFeedbackFlag((ptr)->Id,(ptr)->Sub_Id,FALSE);//clear set Waite_Feedback_Flag
					(ptr)->NobackRetry_cn = 0;
					(ptr)->NobackTimer = 0;
				}

			} 
		}
	}
}

void app_sci1ItfUpdataTxMsgs(void)
{ 
	UINT8 u8_temp=0; 
	UINT16 u16_temp=0;
	UINT32 u32_temp=0;
	SINT16 s16_temp = 0;
	UINT8 u8_last=0; 
	UINT16 u16_last=0;
	UINT32 u32_last=0;
	UINT8 id,txData_len;
	BOOL sub_exist=0;
	static BOOL cpu_ready=FALSE;
	static BOOL sci1_event_msg_firstSend_flag = FALSE;
//   c_u16_wr(&CanMsg4E1_Tx, 38, 10, 0x1ff);
//   c_u8_wr(&CanMsg531_Tx, 0, 8, 0x30);
//   c_bool_wr(&CanMsg531_Tx,8,1,temp);
//  c_bool_wr(&CanMsg531_Tx,15,1,temp);
#if	CAN_RX_USE_TEST
	cpu_ready = TRUE;//ru guo cpu qi dong hao le ,jiang suo you de shi jian bao wen fa song yi ci 。gai chu shang dian zhi yun xing yi ci.
	cpu_ready = TRUE;//ru guo cpu mei qi dong hao ,bing qie chao shi le ,ze yun xing xia mian de cheng xu.

	if(cpu_ready == TRUE)
	{
		sci1ItfGetmessageFromCanMsg123(sci1_event_msg_firstSend_flag);
		sci1ItfGetmessageFromCanMsg124(sci1_event_msg_firstSend_flag);

		sci1_event_msg_firstSend_flag = TRUE;//bao zheng cpu ready zhi hou fa yi ci shi jian bao wen
	}



#endif
	
}

/***********************************************************
	chuan kou buf Get message From CanMsg123 buf，ru guo sci shi shi jian bao wen ,ze dang shi jiu fa song gei CPU.
**********************************************************/
static void sci1ItfGetmessageFromCanMsg123(BOOL sended_flg)
{
	UINT8 u8_temp=0; 
	UINT16 u16_temp=0;
	UINT32 u32_temp=0;
	SINT16 s16_temp = 0;
	UINT8 u8_last=0; 
	UINT16 u16_last=0;
	UINT32 u32_last=0;
	UINT8 id,txData_len;
	BOOL sub_exist=0;

	//////////////////////////CanMsg123_Rx chu li////////////////////////////////////////////////
	u8_last = c_u8_LastData_rd(&CanMsg123_Rx,0,2);//KeySts
	u8_temp = c_u8_rd(&CanMsg123_Rx,0,2);//KeySts
	if(u8_temp != u8_last || (sended_flg==FALSE))
	{
		if(u8_temp == 0)
		{
			s_u8_wr(&SciMsg1C_Tx, 0,1,u8_temp);//biao shi xi tong dian yuan guan bi
			SciMsg1C_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg1C_Tx);//sci The order was issued at that moment if is event order
		}
		else if(u8_temp == 1)
		{
			s_u8_wr(&SciMsg1C_Tx, 0,1,1);//biao shi xi tong dian yuan da kai
			SciMsg1C_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg1C_Tx);//sci The order was issued at that moment if is event order
		}
		else
		{
			s_u8_wr(&SciMsg08_Tx, 0,8,12);//qi che da huo
			SciMsg08_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg08_Tx);//sci The order was issued at that moment if is event order
		}
	}


	
	u8_last = c_bool_LastData_rd(&CanMsg123_Rx,2,1);//RHTurnlightSts
	u8_temp = c_bool_rd(&CanMsg123_Rx,2,1);//RHTurnlightSts
	if(u8_temp != u8_last || (sended_flg==FALSE))
	{
		if(u8_temp == 0)
		{
			s_u8_wr(&SciMsg08_Tx, 0,8,5);//you zhuan xiang deng guan
			SciMsg08_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg08_Tx);
		}
		else if(u8_temp == 1)
		{
			s_u8_wr(&SciMsg08_Tx, 0,8,4);//you zhuan xiang deng kai
			SciMsg08_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg08_Tx);
		}
	}

	u8_last = c_bool_LastData_rd(&CanMsg123_Rx,3,1);// LHTurnlightSts
	u8_temp = c_bool_rd(&CanMsg123_Rx,3,1);// LHTurnlightSts
	if(u8_temp != u8_last || (sended_flg==FALSE))
	{
		if(u8_temp == 0)
		{
			s_u8_wr(&SciMsg08_Tx, 0,8,3);//zuo zhuan xiang deng guan
			SciMsg08_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg08_Tx);
		}
		else if(u8_temp == 1)
		{
			s_u8_wr(&SciMsg08_Tx, 0,8,2);//zuo zhuan xiang deng kai
			SciMsg08_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg08_Tx);
		}
	}

	u8_last = c_bool_LastData_rd(&CanMsg123_Rx,4,1);//ReverseGearSwitch
	u8_temp = c_bool_rd(&CanMsg123_Rx,4,1);//ReverseGearSwitch
	if(u8_temp != u8_last || (sended_flg==FALSE))
	{
		if(u8_temp == 0)
		{
			s_u8_wr(&SciMsg08_Tx, 0,8,1);//tui chu dao che zhuang tai 
			SciMsg08_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg08_Tx);
		}
		else if(u8_temp == 1)
		{
			s_u8_wr(&SciMsg08_Tx, 0,8,0);//jin ru dao che zhuang tai
			SciMsg08_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg08_Tx);
		}
	}

///////////////zhou qi chuan kou bao wen bu yong shi shi fa song ////////////		
	u8_temp = c_bool_rd(&CanMsg123_Rx,18,1);//VehicleSpeedVSOSigValidData
	if(u8_temp == 0)//che su you xiao
	{
		u16_temp = c_u16_rd(&CanMsg123_Rx,19,13);//VehicleSpeed VSOSig
		u16_temp = u16_temp/16;
		s_u16_wr(&SciMsg02_Tx, 0,16,u16_temp);		//zhou qi bao wen bu yong li ma fa song .
	}


	u16_temp = c_u16_rd(&CanMsg123_Rx,34,16);//SteeringAngle
	s16_temp = u16_temp/16 - 2048;
	if(s16_temp > 0)
	{
		s_u8_wr(&SciMsg01_Tx, 0,8,1);
		s_u8_wr(&SciMsg01_Tx, 8,8,s16_temp*10);

	}
	else
	{
		s_u8_wr(&SciMsg01_Tx, 0,8,0);
		s_u8_wr(&SciMsg01_Tx, 8,8,(-s16_temp)*10);
	}

}

static void sci1ItfGetmessageFromCanMsg124(BOOL sended_flg)
{
	UINT8 u8_temp=0; 
	UINT16 u16_temp=0;
	UINT32 u32_temp=0;
	SINT16 s16_temp = 0;
	UINT8 u8_last=0; 
	UINT16 u16_last=0;
	UINT32 u32_last=0;
	UINT8 id,txData_len;
	BOOL sub_exist=0;
	u8_last = c_bool_LastData_rd(&CanMsg124_Rx,1,1);//RHTurnlightSts
	u8_temp = c_bool_rd(&CanMsg124_Rx,1,1);//RHTurnlightSts
	if(u8_temp != u8_last || (sended_flg==FALSE))
	{
		if(u8_temp == 0)
		{
			s_u8_wr(&SciMsg0B_Tx, 0,8,0);//you zhuan xiang deng guan
			SciMsg0B_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg0B_Tx);
		}
		else if(u8_temp == 1)
		{
			s_u8_wr(&SciMsg0B_Tx, 0,8,1);//you zhuan xiang deng guan
			SciMsg0B_Tx.Trigger = TRUE;
			app_sci1ItfEventSent(&SciMsg0B_Tx);

		}
	}

	
}

/*for other interface*/

  
/*****************************************************************************
** End File
*****************************************************************************/
